<?php
class AdminModel extends Model {
	public function __construct() {
		parent::__construct('Admin');
	}
	
	public function addOne(& $data) {
		return $this->data($data)->add();
	}
	
	public function getOneByPk($id) {
		return $this->where('id='.$id)->find();
	}
	
	public function updateOne($where, & $data) {
		return $this->where($where)->save($data);
	}
	
	public function deleteOne($where) {
		return $this->where($where)->delete();
	}
	
	public function getUserByCondition($where) {
		$pre = C('DB_PREFIX');
		$sql = "select * from {$pre}admin where {$where} limit 0,1";
			
		$data = $this->query($sql);
		
		return empty($data) ? array() : $data[0];
	}
	
	public function getListByPage($pageSize = 20) {
		import('ORG.Util.Page');
		$pre = C('DB_PREFIX');
		
		$count = $this->count();	
		if(empty($count)) $count = 0;
		$page = new Page($count, $pageSize);
		$pageString = $page->show();
		
		$sql = "select * from {$pre}admin where id<=(select id from {$pre}admin order by id desc limit {$page->firstRow},1) order by id desc limit {$pageSize}";
		$data = $this->query($sql);
		
		$data['pageString'] = $pageString;
		
		return $data;
	}
	
	public function processLogout() {
		unset($_SESSION['adminUser']);
	}
	
	public function saveUserInfo(& $userData) {
		$_SESSION['adminUser'] = $userData;
	}
}